package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.dao.ReportDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Report;
import com.itheima.pojo.SetMealReport;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author M.F.S
 */
@Service(version = "1.0.0", interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {


    @Resource
    private ReportDao reportDao;

    /**
     * 新增体检报告,修改档案号
     *
     * @param fileNumber
     */
    @Override
    public void add (String fileNumber) {
        reportDao.add(fileNumber);
    }

    /**
     * 文件上传
     *
     * @param list
     */
    @Override
    public void fileUpload(List<String[]> list) {
        ArrayList<Report> reports = new ArrayList<>();
        for (String[] s : list) {
            Report report = new Report();
            report.setFileNumber("-1");
            report.setName(s[1]);
            report.setPETime(s[2]);
            report.setCheckItem(s[3]);
            report.setResult(s[4]);
            report.setCode(s[5]);
            report.setLeftCritical(s[6]);
            report.setRightCritical(s[7]);
            report.setCompany(s[8]);
            report.set_deleted(false);
            reports.add(report);
        }
        //调用Doa层进行对象存储
        for (Report report : reports) {
            reportDao.fileUpload(report);
        }

    }

    /**
     * 会员档案分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //开启分页查询
        PageHelper.startPage(currentPage, pageSize);
        //调用dao层查询语句
        Page<Report> page = reportDao.findPage(queryString);
        List<Report> result = page.getResult();
        //封装分页查询结果对象
        return new PageResult(page.getTotal(), result);
    }

    /**
     * 删除体检报告
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        reportDao.delete(id);
    }

    /**
     * 根据月份查询会员数量
     * @param months
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        ArrayList<Integer> memberCounts = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";
            Integer memberCount = reportDao.findMemberCountByMonths(date);
            memberCounts.add(memberCount);
        }
        return memberCounts;
    }

    /**
     * 饼状图套餐比例
     * @return 查出来的一个套餐对应有多少个预约数量
     */
    @Override
    public Result getSetMealReport() {
        List<SetMealReport> setMealList = reportDao.getSetMealReport();
        return new Result(true,"获取套餐统计数据成功",setMealList);
    }

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        System.out.println("calendar = " + calendar);
        calendar.add(Calendar.MONTH, -12);
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH, 1);
            System.out.println(new SimpleDateFormat("yyyy-MM").format(calendar.getTime()));
        }
    }

}
